<?php

/**
 * This is the model class for table "yii_transport".
 *
 * The followings are the available columns in table 'yii_transport':
 * @property integer $id
 * @property integer $transport_type_id
 * @property integer $sending_point_id
 * @property integer $destination_id
 * @property double $price
 */
class Transport extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Transport the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'yii_transport';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('transport_type_id, sending_point_id, destination_id, price', 'required'),
			array('transport_type_id, sending_point_id, destination_id', 'numerical', 'integerOnly'=>true),
			array('price', 'numerical'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, transport_type_id, sending_point_id, destination_id, price', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'transport_type_id' => 'Тип транспотного средства',
			'sending_point_id' => 'Пункт отправления',
			'destination_id' => 'Пункт назначения',
			'price' => 'Стоимость билета',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('transport_type_id',$this->transport_type_id);
		$criteria->compare('sending_point_id',$this->sending_point_id);
		$criteria->compare('destination_id',$this->destination_id);
		$criteria->compare('price',$this->price);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    /**
     * Retrieves name of current transport type
     * @return name of transport type
     */
    public function getType(){
        return TransportType::model()->findByPk($this->transport_type_id)->name;
    }

    public function getSendingPoint(){
        $sending_point = Locality::model()->findByPk($this->sending_point_id);
        return $sending_point->country.', '.$sending_point->region.', '.$sending_point->city;
    }

   public function getDestinationPoint(){
        $destination_point = Locality::model()->findByPk($this->destination_id);
        return $destination_point->country.', '.$destination_point->region.', '.$destination_point->city;
    }
}